What is the bitwise NOT (~) operator in JavaScript?
What is the bitwise NOT (~) operator in JavaScript?
301
30-Oct-2023
Updated on 31-Oct-2023
Aryan Kumar
31-Oct-2023The bitwise NOT (~) operator in JavaScript is a unary operator that's used to invert the bits of a number. It works by changing each 0 bit to 1, and each 1 bit to 0. It's often used to flip the binary representation of a number.
Here's how you can use the bitwise NOT operator in JavaScript:
In the example above, the ~num operation flips the bits of the number 5 to give you -6 as the result. Just keep in mind that the result is a signed 32-bit integer in two's complement representation, so it may appear as a negative number in some cases.